package com.iqik.docker.controller;

import com.iqik.docker.pojo.User;
import com.iqik.docker.service.UserService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/demo")
public class DemoController {

    private final UserService userService;

    private final RedisTemplate<String, String> redisTemplate;

    @Autowired
    public DemoController(UserService userService, RedisTemplate<String, String> redisTemplate) {
        this.userService = userService;
        this.redisTemplate = redisTemplate;
    }

    @RequestMapping("/mysql")
    public String getUsers() {
        List<User> users = userService.getUsers();
        return JSONObject.toJSONString(users);
    }

    @PostMapping("/redis")
    public String setRedis(String key, String data) {
        redisTemplate.opsForValue().set(key, data);
        return "success";
    }

    @GetMapping("/redis/{key}")
    public String getRedis(@PathVariable(name = "key") String key) {
        return redisTemplate.opsForValue().get(key);
    }
}
